package nowcoder;

import java.util.Arrays;

/**
 * @author wangxi03 created on 2021/6/23 12:02 下午
 * @version v1.0
 *
 * 来自于nowcoder
 *
 * https://www.nowcoder.com/questionTerminal/762836f4d43d43ca9deb273b3de8e1f4
 */
public class IsContinuous {

    public static void main(String[] args) {
        IsContinuous obj = new IsContinuous();
        int[] nums = {1,3,0,5,0};
        boolean continuous = obj.isContinuous(nums);

        System.out.println(continuous);
    }

    public boolean isContinuous(int [] nums) {
        if (nums == null || nums.length <= 0) {
            return false;
        }
        // 排序是因为存在重复元素，过滤重复元素
        // 注意数组最小值可能为 0
        // 其实解法就是 数组最大值 - 数组除0最小值 < 5就行了。
        Arrays.sort(nums);
        int zeroCount = 0;
        for (int i = 0; i < nums.length; i++) {
            // 有可能0元素是重复的
            if (nums[i] == 0) {
                zeroCount++;
                continue;
            }
            if (i > 0 && nums[i] == nums[i - 1]) {
                return false;
            }
        }
        return nums[nums.length - 1] - nums[zeroCount] < 5;
    }
}
